﻿<%@ Control Language="C#" ClassName="HeaderTools" %>

<script runat="server">
  protected void Page_Load(object sender, EventArgs e)
  {
    if (!Page.IsPostBack)
    {
      if (!string.IsNullOrEmpty(HttpContext.Current.User.Identity.Name))
      {
        if (HttpContext.Current.Request.Cookies["LoginInfo"] != null)
        {
          HttpCookie LoginInfo = HttpContext.Current.Request.Cookies["LoginInfo"] as HttpCookie;
          lbl_username.Text = Server.HtmlEncode(HttpContext.Current.Server.UrlDecode(LoginInfo.Values["UserName"].ToString()));
          lbl_authType.Value = LoginInfo.Values["AuthType"].ToString();
        }
      }
      else
      {
        lbl_username.Text = "";
        lbl_authType.Value = "";
      }

      tbKeyword.Attributes.Add("placeholder", "請輸入您要查詢的條件");
    }
  }

  protected void Logout_Click(object sender, EventArgs e)
  {
    System.Web.Security.FormsAuthentication.SignOut();
    Response.Redirect("~/Home.aspx");
  }

  protected void btnSearch_Click(object sender, EventArgs e)
  {
    NameValueCollection data = new NameValueCollection();
    data.Add("Keyword", tbKeyword.Text.Trim());
    HttpHelper.RedirectAndPOST(this.Page,
      Page.ResolveUrl("~/Products/ProductSearchResult.aspx"),
      data);
  }
  
</script>

<script type="text/javascript">

  $(document).ready(function () {

    //alert($.cookie("LoginInfo"));    

    if ($.cookie("chk_rememberValue") == undefined || $.cookie("chk_rememberValue") != "true") {
      $('.OutlinkBox').click(function (e) {
        lightbox($(".popup-content .outlink").prop('outerHTML'));
        //e.preventDefault();
        //e.stopPropagation();
      });
    }

    // 記住卡號
    if ($.cookie("RememberMemberID") != undefined && $.cookie("RememberMemberID").length > 0 ) {
      $("#txt_username").val($.cookie("RememberMemberID"));
    }
    if ($.cookie("RememberEnterMemberID") != undefined && $.cookie("RememberEnterMemberID").length > 0) {
      $("#enterID").val($.cookie("RememberEnterMemberID"));
    }

    var LoginID = $("[id*='lbl_username']").text();
    if (LoginID != "") {
      $(".login-areas").fadeOut(100);
      $(".login-links").fadeOut(100);
      //$("[id*='Logout']").show();
    }
    else {
      //$("[id*='Logout']").hide();
      $("[id*='lb_ConfirmOrder']").click(function (e) {
        lightbox($(".popup-content .member-lightbox").prop('outerHTML'));
        if ($.cookie("RememberMemberID") != undefined && $.cookie("RememberMemberID").length > 0) {
          $(".lightbox #mem-username").val($.cookie("RememberMemberID"));
        }
        if ($.cookie("RememberEnterMemberID") != undefined && $.cookie("RememberEnterMemberID").length > 0) {
          $(".lightbox #ent-username").val($.cookie("RememberEnterMemberID"));
        }
        e.preventDefault();
        e.stopPropagation();
      });
    }
  });

  function OutlinkBox(desUrl) {
    $("#GoUrl").val(desUrl);
    //當已經勾選"下次不要再提醒我"時，直接導向指定網頁
    if ($.cookie("chk_rememberValue") == "true") {
      window.open(desUrl, '_blank');
    }
  }

  function SetChkRremember() {
    var chk_rememberValue = $('.lightbox #chk_remember').is(':checked');
    //設定勾選"下次不要再提醒我"
    $.cookie("chk_rememberValue", chk_rememberValue, { path: "/" });
    //alert("set cookie : " + $.cookie("chk_rememberValue"));
    window.open($("#GoUrl").val(), '_blank');

    lightboxClose();
    //勾選"下次不要再提醒我"，移除Click事件
    if ($.cookie("chk_rememberValue") == "true") {
      $('.OutlinkBox').unbind("click");
    }
  }

  function MemberAuthenticate() {
    var userID = $("#txt_username").val();
    var userPW = $("#txt_password").val();
    var RememberMe = $('#chk_RememberMe').is(':checked');
    if (RememberMe) {
      $.cookie("RememberMemberID", userID, { path: "/", expires: 5 });
    }
    else {
      $.removeCookie("RememberMemberID", { path: "/", expires: 5 });
    }
    Authenticate(userID, userPW, RememberMe, "1", "");
  }

  function EnterpriseAuthenticate() {
    var userID = $("#enterID").val();
    var userPW = $("#enterName").val();
    var RememberMe = $('#chk_EnterRememberMe').is(':checked');
    if (RememberMe) {
      $.cookie("RememberEnterMemberID", userID, { path: "/", expires: 5 });
    }
    else {
      $.removeCookie("RememberEnterMemberID", { path: "/", expires: 5 });
    }
    Authenticate(userID, userPW, RememberMe, "2", "");
  }

  function LightboxMemberAuthenticate() {
    var userID = $(".lightbox #mem-username").val();
    var userPW = $(".lightbox #mem-password").val();
    var RememberMe = $('.lightbox #mem-RememberMe').is(':checked');
    if (RememberMe) {
      $.cookie("RememberMemberID", userID, { path: "/", expires: 5 });
    }
    else {
      $.removeCookie("RememberMemberID", { path: "/", expires: 5 });
    }
    Authenticate(userID, userPW, RememberMe, "1", "order");
  }

  function LightboxEnterpriseAuthenticate() {
    var userID = $(".lightbox #ent-username").val();
    var userPW = $(".lightbox #ent-password").val();
    var RememberMe = $('.lightbox #ent-RememberMe').is(':checked');
    Authenticate(userID, userPW, RememberMe, "2", "order");
  }

  function LightboxNonMemberAuthenticate() {
    var nonMemberID = $(".lightbox #nonMemberID").val();
    var nonMemberType = $(".lightbox input[name='rb_nonMemberType']:checked").val();

    if (nonMemberType == "4") {
      if (nonMemberID == "") {
        alert("請輸入身分證字號!");
        return false;
      }
      else {
        if (CheckTWID(nonMemberID) == false) {
          alert('請輸入正確的身分證字號!');
          return false;
        }
      }
      $.cookie("TWIdentity", nonMemberID.toUpperCase(), { path: "/" });
    }
    else {
      $.removeCookie('TWIdentity', { path: '/' });
      $.removeCookie('LoginInfo', { path: '/' });
    }
    window.location.href = "<%= Page.ResolveUrl("~/Orders/OrderSummaryAndAmount.aspx")%>";
  }



  function Authenticate(userID, userPW, RememberMe, authType, authMethod, ActiveTab) {
    if (typeof (ActiveTab) == 'undefined') ActiveTab = '1';
    $.ajax({
      type: "POST",
      url: "<%= Page.ResolveUrl("~/Login.aspx/Authenticate")%>",
      data: "{'username': '" + userID
        + "','password': '" + userPW
        + "','rememberMe':'" + RememberMe
        + "','authType':'" + authType + "'}",
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: function (data, status) {
        if (data.d != '-1') {
          var result = JSON.parse(data.d); //取得回傳的Json資料

          var Is_Valid = result.Is_Valid; //取得Json第一個值 Is_Valid
          var Msg = result.Msg; //取得Json第二個值 Msg

          if (Is_Valid == "Y") {

            //只有會員才會顯示登入後的會員名稱
            if (authType == "1") {
              var pUserInfo_Ary = result.pUserInfo_Ary;
              if (pUserInfo_Ary["UserName"] == "") {
                $("[id*='lbl_username']").text("親愛的會員");
              }
              else {
                $("[id*='lbl_username']").text(pUserInfo_Ary["UserName"]);
              }
              $("[id*='lbl_authType']").val(authType);
              $("[id*='mix_radio_mileage']").removeAttr("disabled");
            }
            else if (authType == "2") {
              $("[id*='lbl_username']").text("親愛的會員");
              $("[id*='lbl_authType']").val(authType);
            }

            $(".login-areas").fadeOut(100);
            $(".login-links").fadeOut(100);

            $("[id*='lb_ConfirmOrder']").off('click');

            //$("[id*='Logout']").show();
            alert("登入成功!");

            lightboxClose();

            $.removeCookie('TWIdentity', { path: '/' });

            if (authMethod == "order") {
              window.location.href = "<%= Page.ResolveUrl("~/Orders/OrderSummaryAndAmount.aspx")%>";
            }
            else if (authMethod == "account") {
              window.location.href = "<%= Page.ResolveUrl("~/Accounts/Account.aspx?ActiveTab=")%>" + ActiveTab;
            }
        }
        else {
            //$("[id*='MemberMsg']").html(Msg);  
            //$("[id*='lbl_username']").text("");
          alert(Msg);
        }
      } else {
          //$("[id*='MemberMsg']").html("會員登入失敗!");          
        alert("會員登入失敗!");

      }
      },
      error: function (xhr, ajaxOptions, thrownError) {
        //$("[id*='MemberMsg']").html("會員登入失敗!");        
        //alert(JSON.stringify(xhr));
      }
    });
};
</script>


<asp:HiddenField ID="RememberOutlink" runat="server" ClientIDMode="Static" />
<asp:HiddenField ID="GoUrl" runat="server" ClientIDMode="Static" />
<div class="tools">
  <div class="welcome">
    <asp:Label ID="lbl_username" runat="server" Text="" />
    您好!
    <asp:LinkButton ID="Logout" runat="server" Text="登出"
      ClientIDMode="Static" Visible="false"></asp:LinkButton>
    <asp:HiddenField ID="lbl_authType" runat="server" />
  </div>
  <div class="search">
    <asp:TextBox ID="tbKeyword" runat="server"></asp:TextBox><asp:Button ID="btnSearch" runat="server" OnClick="btnSearch_Click" />
  </div>
  <div class="login-links">
    <a class="JS-member-login-btn" href="#">會員登入</a>
    <a href="#" class="OutlinkBox" onclick="OutlinkBox('https://eservice.evaair.com/evaweb/eva/ffp/register-now.aspx?lang=zh-tw');">加入會員</a>
    <a class="JS-bussinese-login-btn" href="#">企業優惠</a>
  </div>
  <div class="login-areas">
    <div class="login-box">
      <div class="member-login">
        <table>
          <tr>
            <th>卡號 / 別名</th>
            <td>
              <asp:TextBox ID="txt_username" runat="server" ClientIDMode="Static" Text=""></asp:TextBox>
            </td>
            <td>
              <a href="#" class="OutlinkBox" onclick="OutlinkBox('https://eservice.evaair.com/Evaweb/EVA/FFP/membership-number-inquiry.aspx?lang=zh-tw');">忘記卡號 / 忘記別名</a>
            </td>
          </tr>
          <tr>
            <th>密碼
            </th>
            <td>
              <asp:TextBox ID="txt_password" runat="server" TextMode="Password" ClientIDMode="Static" Text=""></asp:TextBox>
            </td>
            <td>
              <a href="#" class="OutlinkBox" onclick="OutlinkBox('https://eservice.evaair.com/Evaweb/EVA/FFP/forgotten-password.aspx?lang=zh-tw');">忘記密碼</a>
            </td>
          </tr>
          <tr>
            <td colspan="3">
              <input type="checkbox" id="chk_RememberMe" name="chk_RememberMe" />
              記住我的卡號/別名 
              <%--<asp:Label ID="MemberMsg" runat="server" EnableViewState="False" ForeColor="Red" />--%>
            </td>
          </tr>
        </table>
        <%--<input class="btn" type="submit" name="name" value="登 入" />--%>
        <asp:Button ID="btnLogin" class="btn" runat="server" Text="登 入" OnClientClick="MemberAuthenticate(); return false;" />
      </div>
    </div>
    <div class="login-box">
      <div class="member-login">
        <table>
          <tr>
            <th>企業編號</th>
            <td>
              <input type="text" name="enterID" id="enterID" value="" />
            </td>
          </tr>
          <tr>
            <th>密碼
            </th>
            <td>
              <input type="password" name="enterName" id="enterName" value="" />
            </td>
          </tr>
          <tr>
            <td colspan="2">
              <div class="fl">
                <input type="checkbox" id="chk_EnterRememberMe" name="chk_EnterRememberMe" />
                <span>記住我的企業編號</span>
              </div>
              <%--<a href="#" class="help fr">HELP</a>--%>
            </td>
          </tr>
        </table>
        <input class="btn" type="submit" name="name" value="登 入" onclick="EnterpriseAuthenticate(); return false;" />
      </div>
    </div>
  </div>
</div>

<div style="display: none" class="popup-content">
  <!-- Outlink Start -->
  <div class="outlink">
    <div class="notice">
      <h4>您即將連結到長榮樂e購以外的網站。</h4>
      <input type="checkbox" id="chk_remember" name="chk_remember" />
      下次不要再提醒我
    </div>
    <div class="liner">
    </div>
    <div class="links">
      <asp:LinkButton ID="btnGO" runat="server" class="btn"
        OnClientClick="SetChkRremember(); return false;"
        ClientIDMode="Static">確定前往</asp:LinkButton>
      <a href="#" class="btn cancelbtn">取消前往</a>
    </div>
  </div>
  <!-- #Outlink End -->

  <!-- Member login Start -->
  <div class="member-lightbox">
    <h4>請選擇下列其一方式登入</h4>
    <div class="liner dotted"></div>
    <div class="login-links JS-login-links">
      <a class="btn" href="#">會員登入</a>
      <a class="btn" href="#">非會員</a>
      <a class="btn" href="#">企業優惠</a>
    </div>
    <div class="tab-areas">
      <div class="area">
        <table>
          <tr>
            <th>卡號 / 別名</th>
            <td>
              <input id="mem-username" type="text" name="mem-username" value="" tabindex="1" />
            </td>
            <td>
              <%--<a href="#" class="OutlinkBox" onclick="OutlinkBox('https://eservice.evaair.com/Evaweb/EVA/FFP/membership-number-inquiry.aspx?lang=zh-tw');">忘記卡號 / 忘記別名</a>--%>
              <a href="https://eservice.evaair.com/Evaweb/EVA/FFP/membership-number-inquiry.aspx?lang=zh-tw" target="_blank">忘記卡號 / 忘記別名</a>
            </td>
            <td class="v-a-b" rowspan="3">
              <input class="btn fr" id="btn-mem-login" tabindex="4"
                onclick="LightboxMemberAuthenticate();" type="submit" name="btn-mem-login" value="登 入" /></td>
          </tr>
          <tr>
            <th>密碼
            </th>
            <td>
              <input id="mem-password" type="password" name="mem-password" value="" tabindex="2" />
            </td>
            <td>
              <%--<a href="#" onclick="OutlinkBox('https://eservice.evaair.com/Evaweb/EVA/FFP/forgotten-password.aspx?lang=zh-tw');">忘記密碼</a>--%>
              <a href="https://eservice.evaair.com/Evaweb/EVA/FFP/forgotten-password.aspx" target="_blank">忘記密碼</a>
            </td>
          </tr>
          <tr>
            <td colspan="3">
              <input type="checkbox" id="mem-RememberMe" name="mem-RememberMe" tabindex="3" />
              記住我的卡號/別名
              <%--<asp:Label ID="mem-message" runat="server" EnableViewState="False" ForeColor="Red" />--%>
            </td>
          </tr>
        </table>
      </div>
      <div class="area">
        <table>
          <tr>
            <th>
              <input type="radio" name="rb_nonMemberType" value="4" checked="checked" tabindex="5" />
              身份證字號
            </th>
            <td>
              <input class="sp" type="text" id="nonMemberID" name="nonMemberID" value="" tabindex="6" maxlength="10" /></td>
          </tr>
          <tr>
            <th class="normal" colspan="2">
              <input type="radio" name="rb_nonMemberType" value="5" tabindex="7" />
              不填身份證字號，直接進行結帳</th>
          </tr>
        </table>
        <h4 class="notice fixed">若您未輸入身份證字號將無法獲得符合優惠活動所贈送之E-Coupon。</h4>
        <div class="info fl">
          <h5>邀您加入無限萬哩遊</h5>
          <p>
            您尚未加入會員嗎? 
          </p>
          <p>請<a href="#" class="orange-text" onclick="javascript:window.open('https://eservice.evaair.com/evaweb/eva/ffp/register-now.aspx?lang=zh-tw', '_blank');" tabindex="8">點選此</a>立即加入貴賓行列</p>
        </div>
        <input class="btn fr" type="submit" id="btn-non-login" name="btn-non-login" onclick="LightboxNonMemberAuthenticate();" value="確 定" tabindex="9" />
      </div>
      <div class="area">
        <table>
          <tr>
            <th>企業編號</th>
            <td>
              <input id="ent-username" type="text" name="ent-username" value="" tabindex="1" />
            </td>
            <td class="v-a-b" rowspan="3">
              <input class="btn fr" id="btn-ent-login" onclick="LightboxEnterpriseAuthenticate();"
                type="submit" name="btn-ent-login" value="登 入" tabindex="5" />
            </td>
          </tr>
          <tr>
            <th>密碼
            </th>
            <td>
              <input id="ent-password" type="password" name="ent-password" value="" tabindex="2" />
            </td>
          </tr>
          <tr>
            <th colspan="2">
              <input type="checkbox" id="ent-RememberMe" name="ent-RememberMe" tabindex="3">
              記住我的企業編號
              <%--<a href="#" class="help margined" tabindex="4">HELP</a>--%>
            </th>
          </tr>
        </table>
      </div>
    </div>
  </div>
  <!-- Member login End -->

  <!-- Message success -->
  <div class="message-success">
    <div class="notice">
      <h4>會員登入成功!!!</h4>
    </div>
  </div>
  <!-- #Message success End -->
  <!-- Message error -->
  <div class="message-error">
    <div class="notice">
      <h4>會員登入失敗!!!</h4>
    </div>
  </div>
  <!-- #Message error End -->
</div>
